//
//  GolfFriendCircleCell.m
//  GolfFriend
//
//  Created by vescky on 13-11-30.
//  Copyright (c) 2013年 vescky.org. All rights reserved.
//

#import "GolfFriendCircleCell.h"
#import "LitePhotoCollectionView.h"
#import "UIImageView+MJWebCache.h"
#import "DateUtility.h"
#import "CommonUtility.h"


@interface GolfFriendCircleCell ()<LitePhotoCollectionViewDelegate> {
    LitePhotoCollectionView *imgCollectionView;
    LitePhotoCollectionView *videoCollectionView;
}

@end

@implementation GolfFriendCircleCell
@synthesize delegate;

- (void)setDataInfo:(CircleContentVO*)ccVO {
    bool hasContent = NO;
    NSArray *imgArr,*videoArr;
    
    if (ccVO.userId && ccVO.userId.length > 0) {
        [imgvAvatar setImageURLStr:getAvartaLinkFromId(ccVO.userId, NO) placeholder:[UIImage imageNamed:@"timeline_image_loading.png"]];
    }
    
    if (ccVO.userName && ccVO.userName.length > 0) {
        [labelUserName setText:ccVO.userName];
    }
    else {
        [labelUserName setText:@"匿名"];
    }
    
    [labelZan setText:ccVO.zan];
    [labelCommons setText:ccVO.comments];
    [labelTime setText:getWeiboDisplayTimeFromDate(getDateFromString(@"yyyy-MM-dd HH:mm:ss", ccVO.time))];
    
    if (ccVO.content && ccVO.content.length > 0) {
        [tvContent setText:ccVO.content];
        hasContent = YES;
    }
    
    if (ccVO.sourceLinks && [ccVO.sourceLinks count] > 0) {
        imgArr = [ccVO.sourceLinks objectForKey:@"image"];
        videoArr = [ccVO.sourceLinks objectForKey:@"video"];
    }
    
    CGRect contetRect = tvContent.frame;
    CGRect allRect = labelAllContent.frame;
    CGRect coverBtnRect = contentCoverButton.frame;
    float flexibleMargin = 0;
    
    if (!hasContent) {
        [tvContent removeFromSuperview];
        contetRect.size.height = 0;
        contetRect.origin.y = labelUserName.frame.origin.y + labelUserName.frame.size.height;
        [labelAllContent removeFromSuperview];
        allRect.size.height = 0;
        allRect.origin.y = contetRect.origin.y;
        [contentCoverButton removeFromSuperview];
        coverBtnRect.size.height = 0;
        coverBtnRect.origin.y = contetRect.origin.y;
    }
    else {
        CGSize contentStringSize = GetStringSize(tvContent.text, 13.0, nil, CGSizeMake(contetRect.size.width, 2000));
        
        if (contentStringSize.height < contetRect.size.height) {
            //可以显示齐全了,去掉"全文"字样
            [labelAllContent removeFromSuperview];
            allRect.size.height = 0;
            
            contetRect.size.height = contentStringSize.height;
            allRect.origin.y = contetRect.origin.y + contetRect.size.height + 10;
            coverBtnRect.size.height = allRect.origin.y + allRect.size.height;
        }
    }
    
    if (imgArr && [imgArr count] > 0) {
        imgCollectionView = [[LitePhotoCollectionView alloc] init];
        [imgCollectionView setPhotoCollection:[[NSMutableArray alloc] initWithArray:imgArr]];
        [imgCollectionView setMargin:10.0];
        [imgCollectionView setViewWidth:contetRect.size.width];//与content同宽度
        [imgCollectionView setColCount:3];
        [imgCollectionView.view setFrame:CGRectMake(contetRect.origin.x, allRect.origin.y + allRect.size.height + flexibleMargin, contetRect.size.width, [imgCollectionView getViewHeight])];
        [self addSubview:imgCollectionView.view];
    }
    /*
    if (videoArr && [videoArr count]) {
        videoCollectionView = [[LitePhotoCollectionView alloc] init];
        [videoCollectionView setPhotoCollection:[[NSMutableArray alloc] initWithArray:imgArr]];
        [videoCollectionView setMargin:10.0];
        [videoCollectionView setViewWidth:contetRect.size.width];//与content同宽度
        [videoCollectionView setColCount:1];
        if (imgCollectionView) {
            [videoCollectionView.view setFrame:CGRectMake(imgCollectionView.view.frame.origin.x, imgCollectionView.view.frame.origin.y + imgCollectionView.view.frame.size.height + flexibleMargin, imgCollectionView.view.frame.size.width, [videoCollectionView getViewHeight])];
        }
        else {
           [videoCollectionView.view setFrame:CGRectMake(contetRect.origin.x, contetRect.origin.y + contetRect.size.height, contetRect.size.width, [imgCollectionView getViewHeight])]; 
        }
        
        [self addSubview:videoCollectionView.view];
    }*/
    
    CGRect viewRect = self.frame;
    viewRect.size.height = allRect.origin.y + allRect.size.height + buttomView.frame.size.height + 3 * flexibleMargin;
    if (imgCollectionView) {
        viewRect.size.height = viewRect.size.height + [imgCollectionView getViewHeight];
    }
    if (videoCollectionView) {
        viewRect.size.height = viewRect.size.height + [videoCollectionView getViewHeight];
    }
    [self setFrame:viewRect];
    
//    CGRect btRect = buttomView.frame;
//    btRect.origin.y = viewRect.size.height - btRect.size.height;
//    [buttomView setFrame:btRect];
}



#pragma mark - LitePhotoCollectionViewDelegate
- (void)imageDidTapped:(int)index {
    
}

#pragma mark - Button Action
- (IBAction)btnGotoDetails:(id)sender {
    if ([delegate respondsToSelector:@selector(detailConteButtonDidPress)]) {
        [delegate detailConteButtonDidPress];
    }
}

- (IBAction)btnAvatarPressed:(id)sender {
    if ([delegate respondsToSelector:@selector(avatarButtonDidPress)]) {
        [delegate avatarButtonDidPress];
    }
}

#pragma mark - Public Methods
- (float)getContentHeight:(CircleContentVO*)cContent {
    [self setDataInfo:cContent];
    return self.frame.size.height;
}



@end
